import { Request, Response, NextFunction } from 'express'
import {IDogDocument,Dog}  from "../models/dog"

export const list = async (req: Request, res: Response, next: NextFunction) => {
    try{
        let {category}=req.query;
        let offset:any= req.query.offset;
        let limit:any= req.query.limit;
        offset = isNaN(offset)?0:parseInt(offset);
        limit = isNaN(limit)?5:parseInt(limit);
        let query:Partial<IDogDocument>={} as IDogDocument;
        if(category && category!="全部") query.category=category as string;//全部不需要传条件
        let total=await Dog.count(query);
        let list=await Dog.find(query).skip(offset).limit(limit).sort({order:1}); 
        setTimeout(()=>{
            res.json({ success: true, data: {
                list,
                hasMore:total> offset+limit
            } });
        },500)
    }
    catch(e){
        res.json({ success: false, data: [] });
    }
}

export const get = async (req: Request, res: Response, next: NextFunction) => {
    try{
        let id=req.params.id;
        let dog:IDogDocument |null= await Dog.findById(id);
        res.json({ success: true, data: dog });
    }catch(e){
        res.json({ success: false, data: [] });
    }

}
